package com.nf.health.app.intervention_details.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.DateUtil;

import com.google.gson.Gson;
import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.QuestionAdapter;
import com.nf.health.app.models.Question;
import com.nf.health.app.models.RemindDetail;
import com.nf.health.app.utils.ViewUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 健康问卷追踪
 * @author jiax
 *
 */
public class HealthSurveyActivity extends BaseActivity {

	public static final String EXTRA_MSG_ID = "id";

	private Map<Integer,String> map;

	private TextView tv_doctor_name;
	private TextView tv_hospital;
	private TextView tv_time;
	private ListView listview;
	private QuestionAdapter adapter;

	private EditText edittext_health;

	private Button button_health;
	private TextView tv_remain;
	List<Question> question;

	String id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		id = getIntent().getStringExtra(EXTRA_MSG_ID);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_health_problem);
		ViewUtil.setText(this, R.id.titlebar_title_tv,"健康追踪问卷");
		tv_doctor_name = ViewUtil.getView(this, R.id.tv_doctor_name);
		tv_hospital = ViewUtil.getView(this, R.id.tv_hospital);
		tv_time = ViewUtil.getView(this, R.id.tv_time);
		button_health = (Button) findViewById(R.id.button_health);
		tv_remain = (TextView) findViewById(R.id.tv_remain);
		edittext_health = (EditText) findViewById(R.id.edittext_health);

		listview = ViewUtil.getView(this, R.id.listview_health);
		map = new HashMap<Integer, String>();
		provider.requestGetRemindDetail(id,"getRemindDetail");
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		if(actionName.equals("getRemindDetail")){
			RemindDetail detail = (RemindDetail)object;
			tv_time.setText(DateUtil.stampToDate(detail.getTime(),"yyyy-MM-dd"));
			tv_doctor_name.setText(detail.getUsername());
			tv_hospital.setText(detail.getHospitalname());
			question = detail.getQuestion();

			if(question.get(0).getAnswer().equals("2") || question.get(0).getAnswer().equals("1")){//答案为空  不操作  不为空 隐藏按钮

				if(TextUtils.isEmpty(detail.getRemarks())){
					tv_remain.setVisibility(View.GONE);
					edittext_health.setVisibility(View.GONE);
				}else{
					tv_remain.setVisibility(View.VISIBLE);
					edittext_health.setVisibility(View.VISIBLE);
					edittext_health.setBackgroundColor(getResources().getColor(R.color.white));
					edittext_health.setText(detail.getRemarks());
				}
				edittext_health.setEnabled(false);
				edittext_health.setClickable(false);
				button_health.setVisibility(View.GONE);
			}else{
				String teye = detail.getStatus();
				if(teye.equals("3")){
					button_health.setVisibility(View.GONE);
					edittext_health.setText(detail.getRemarks());
					edittext_health.setEnabled(false);
					edittext_health.setClickable(false);
				} else {
					button_health.setVisibility(View.VISIBLE);
				}
			}
			adapter = new QuestionAdapter(HealthSurveyActivity.this,question);
			listview.setAdapter(adapter);
		}
		if(actionName.equals("HealthQuestion")){
			showToast("提交成功！");
			finish();
		}
		adapter.setOnMyClickListener(new OnMyClickListener() {
			@Override
			public void onClick(int position, String flag) {
				// TODO Auto-generated method stub
				map.put(position,flag);
			}
		});
		button_health.setOnClickListener(new OnClickListener() {//完成按钮
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String json = new Gson().toJson(map);
				if(map.size() < question.size()){
					showToast("请选择答案后,点击完成");
					return;
				}else{
					String leave = edittext_health.getText().toString();
					provider.requesthealthQuestion(leave,json,id,"HealthQuestion");
				}
			}
		});
	}
}
